/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package java_project;

import JavaSource.Coupon;
import JavaSource.Customer;
import JavaSource.FoodOrderingSystem;
import JavaSource.Member;
import JavaSource.Order;
import JavaSource.Payment;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Xiaosi Xu
 */
public class PaymentGUI extends javax.swing.JFrame {

    /**
     * Creates new form PaymentGUI
     */
    public PaymentGUI() {
        initComponents();
    }
   
    
    //Initiate Payment GUI
    public PaymentGUI (FoodOrderingSystem fos, Customer cus, Order order ) {
         this.fos = fos;
         this.currentCust = cus;
         this.order = order;
         initComponents();
         if(currentCust instanceof Member){ 
             this.jLabel7.setText(Integer.toString(((Member)currentCust).getRewardCredits()));
             totalLable.setText(Double.toString(order.getTotalPrice()));
             
             
         }
         else{
             jRadioButton2.setVisible(false);
             existCard.setVisible(false);
             existCombo.setVisible(false);
             addCard.setVisible(false);
             newCardNoText.setVisible(false);
             jLabel3.setVisible(false);
             
             jLabel4.setVisible(false);
             jLabel7.setVisible(false);
             jLabel8.setVisible(false);
             creditApplyText.setVisible(false);
             calculateButton.setVisible(false);
             jLabel5.setVisible(false);
             MoneyDeductedLable.setVisible(false);
             applyCreditButton.setVisible(false);
         }
         
         this.totalLable.setText(Double.toString(order.getTotalPrice()));
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        buttonGroup3 = new javax.swing.ButtonGroup();
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jPanel1 = new javax.swing.JPanel();
        existCard = new javax.swing.JRadioButton();
        addCard = new javax.swing.JRadioButton();
        existCombo = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        newCardNoText = new javax.swing.JTextField();
        CouponButton = new javax.swing.JButton();
        applyCreditButton = new javax.swing.JButton();
        confirmButton = new javax.swing.JButton();
        couponText = new javax.swing.JTextField();
        creditApplyText = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        calculateButton = new javax.swing.JButton();
        MoneyDeductedLable = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel9 = new javax.swing.JLabel();
        totalLable = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel6 = new javax.swing.JLabel();
        pickUpButton = new javax.swing.JRadioButton();
        DelieveryButton = new javax.swing.JRadioButton();
        jLabel10 = new javax.swing.JLabel();
        addText = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setType(java.awt.Window.Type.POPUP);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 16)); // NOI18N
        jLabel1.setText("Make Payment");

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel2.setText("Choose Payment Method:");

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        jRadioButton1.setText("Cash");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jRadioButton2.setText("Debit/Credit Card");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jPanel1, org.jdesktop.beansbinding.ELProperty.create("${selection.enabled}"), jRadioButton2, org.jdesktop.beansbinding.BeanProperty.create("selected"));
        bindingGroup.addBinding(binding);

        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jRadioButton2, org.jdesktop.beansbinding.ELProperty.create("${selection.selected}"), jPanel1, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        bindingGroup.addBinding(binding);

        buttonGroup3.add(existCard);
        existCard.setText("Choose Existing");
        existCard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                existCardActionPerformed(evt);
            }
        });

        buttonGroup3.add(addCard);
        addCard.setText("Add New Card");

        jPanel2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel3.setText("Card Number:");

        newCardNoText.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(newCardNoText, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(109, 109, 109))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(newCardNoText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addCard)
                    .addComponent(existCard)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(existCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(40, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(existCard)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(existCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(addCard)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(93, 93, 93))
        );

        existCard.getAccessibleContext().setAccessibleParent(jRadioButton2);

        CouponButton.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        CouponButton.setText("Apply Coupon");
        CouponButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CouponButtonActionPerformed(evt);
            }
        });

        applyCreditButton.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        applyCreditButton.setText("Apply Reward Credit");
        applyCreditButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyCreditButtonActionPerformed(evt);
            }
        });

        confirmButton.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        confirmButton.setText("Confirm Payment");
        confirmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmButtonActionPerformed(evt);
            }
        });

        couponText.setForeground(new java.awt.Color(204, 204, 204));
        couponText.setText("HD38957879HC");
        couponText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                couponTextActionPerformed(evt);
            }
        });

        creditApplyText.setText("                                        ");

        jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel5.setText("Money Deducted:  $");

        calculateButton.setText("Calculate it!");
        calculateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calculateButtonActionPerformed(evt);
            }
        });

        MoneyDeductedLable.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        MoneyDeductedLable.setText("00.00");

        jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel4.setText("Credit Earned: ");

        jLabel7.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel7.setText("0000");

        jLabel8.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel8.setText("Credit will apply:");

        jLabel9.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel9.setText("Total: $");

        totalLable.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        totalLable.setText("00.00");

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel6.setText("Dining Method:");

        buttonGroup2.add(pickUpButton);
        pickUpButton.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        pickUpButton.setText("Pick up");
        pickUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pickUpButtonActionPerformed(evt);
            }
        });

        buttonGroup2.add(DelieveryButton);
        DelieveryButton.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        DelieveryButton.setText("Delievery");
        DelieveryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DelieveryButtonActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jLabel10.setText("Edit Address:");

        addText.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(confirmButton)
                .addGap(24, 24, 24))
            .addComponent(jSeparator2)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(couponText, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(CouponButton, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19))
            .addComponent(jSeparator3)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(207, 207, 207)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton1)
                            .addComponent(jLabel2)
                            .addComponent(jRadioButton2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(totalLable))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(creditApplyText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(calculateButton))
                                    .addComponent(MoneyDeductedLable)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(248, 248, 248)
                                        .addComponent(applyCreditButton))))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel10))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(pickUpButton)
                                .addGap(18, 18, 18)
                                .addComponent(DelieveryButton))
                            .addComponent(addText, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CouponButton)
                    .addComponent(couponText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(creditApplyText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(calculateButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(MoneyDeductedLable)
                    .addComponent(applyCreditButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(totalLable))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jRadioButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(pickUpButton)
                            .addComponent(DelieveryButton))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel10))
                    .addComponent(addText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(57, 57, 57)
                .addComponent(confirmButton)
                .addContainerGap())
        );

        jPanel1.getAccessibleContext().setAccessibleParent(jRadioButton2);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CouponButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CouponButtonActionPerformed
        // TODO add your handling code here:
        
        Coupon ccoup = fos.searchCoupon(this.couponText.getText().toString());
        
        if(ccoup != null) {
            double rate = ccoup.getRate();
            Date begin = ccoup.getBeginTime();
            Date exp =ccoup.getExpirationTime();
            Date date = new Date();
            
            if(date.before(exp)&& date.after(begin))
            {
                System.out.println(order.getTotalPrice());
                double totalAmount = (order.getTotalPrice())*(1-rate);   
                this.totalLable.setText(totalAmount+"");
                System.out.println( (order.getTotalPrice())*(1-rate));
                JOptionPane.showMessageDialog(null, "Coupon apply successfully!");
            } else{
            
                JOptionPane.showMessageDialog(null, "Coupon expired!");
            }
               
        } else {
            JOptionPane.showMessageDialog(null, "Coupon invalid!");
        }
    }//GEN-LAST:event_CouponButtonActionPerformed

    private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calculateButtonActionPerformed
        // TODO add your handling code here;
        boolean isNumber = IsInt_ByException(this.creditApplyText.getText());
        if(!isNumber)
        {
            JOptionPane.showMessageDialog(null, "Please type in a whole number");
            
        }else if(Integer.parseInt(this.creditApplyText.getText()) > Integer.parseInt(this.jLabel7.getText())){
            
            JOptionPane.showMessageDialog(null, "Number of redit applied cannot be larger than the credit earned!");
            
        }else
        {
            int apply = Integer.parseInt(this.creditApplyText.getText());
            double deducted = apply / 50.00;
            this.MoneyDeductedLable.setText(Double.toString(deducted));
        }
        
    }//GEN-LAST:event_calculateButtonActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // TODO add your handling code here:
        existCard.setEnabled(true);
        existCombo.setEnabled(true);
        addCard.setEnabled(true);
        newCardNoText.setEnabled(true);
        jLabel3.setEnabled(true);
        
        int length = ((Member)currentCust).getCardInfo().length;
        String[] cust = ((Member)currentCust).getCardInfo();
        for (int i = 0; i<length; i++){
            if(cust[i] != null){
                this.existCombo.addItem(cust[i]);
            }
        }
        this.newCardNoText.setText(null);
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        // TODO add your handling code here:
        existCard.setEnabled(false);
        existCombo.setEnabled(false);
        addCard.setEnabled(false);
        newCardNoText.setEnabled(false);
        jLabel3.setEnabled(false);
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void couponTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_couponTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_couponTextActionPerformed

    private void applyCreditButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyCreditButtonActionPerformed
        // TODO add your handling code here:
        double moneyDeducted = Double.parseDouble(this.MoneyDeductedLable.getText());
        double total = order.getTotalPrice() - moneyDeducted;
        order.setTotalPrice(total);
        this.totalLable.setText(Double.toString(total));
        
        JOptionPane.showMessageDialog(null, "Apply Reward Credit Successfully!");
    }//GEN-LAST:event_applyCreditButtonActionPerformed

    private void confirmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmButtonActionPerformed
        // TODO add your handling code here:
        if(jRadioButton1.isSelected()){
            double total = Double.parseDouble(this.totalLable.getText());
            Date date = new Date();
            int id = currentCust.getCustomerID();
            Payment pmt = new Payment(total, date, id);
            fos.getPaymentList().add(pmt);
            JOptionPane.showMessageDialog(null, "Payment made Successfully!");
        }else if(existCard.isSelected()){
            double total = Double.parseDouble(this.totalLable.getText());
            Date date = new Date();
            int id = currentCust.getCustomerID();
            Payment pmt = new Payment(total, date, id);
            fos.getPaymentList().add(pmt);
            
            int previousCredit = ((Member)currentCust).getRewardCredits();
            int creditApply = Integer.parseInt(this.creditApplyText.getText());
            ((Member)currentCust).updateRewardCredit(total, previousCredit, creditApply);
            
            JOptionPane.showMessageDialog(null, "Payment made Successfully!");
        }else if(addCard.isSelected()){
            double total = Double.parseDouble(this.totalLable.getText());
            Date date = new Date();
            int id = currentCust.getCustomerID(); 
            Payment pmt = new Payment(total, date, id);
            fos.getPaymentList().add(pmt);
            ((Member)currentCust).setCardInfo(newCardNoText.getText());
            
            int previousCredit = ((Member)currentCust).getRewardCredits();
            int creditApply = Integer.parseInt(this.creditApplyText.getText());
            ((Member)currentCust).updateRewardCredit(total, previousCredit, creditApply);
            
            JOptionPane.showMessageDialog(null, "Payment made Successfully!");
        }else{
            JOptionPane.showMessageDialog(null, "Payment failed!");
        }
        
        if(this.DelieveryButton.isSelected()){
            currentCust.setDeliveryAddress(this.addText.getText());
        }
        
        this.dispose();
    }//GEN-LAST:event_confirmButtonActionPerformed

    private void existCardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_existCardActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_existCardActionPerformed

    private void pickUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pickUpButtonActionPerformed
        // TODO add your handling code here:
        this.jLabel10.setEnabled(false);
        this.addText.setEnabled(false);
    }//GEN-LAST:event_pickUpButtonActionPerformed

    private void DelieveryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DelieveryButtonActionPerformed
        // TODO add your handling code here:
        this.jLabel10.setEnabled(true);
        this.addText.setEnabled(true);
        this.addText.setText(currentCust.getDeliveryAddress());
    }//GEN-LAST:event_DelieveryButtonActionPerformed
    
    //To verify if the string could convert to a number
    public boolean IsInt_ByException(String str)
    {
        try
        {
            Integer.parseInt(str);
            return true;
        }
        catch(NumberFormatException nfe)
        {
            return false;
        }
    }
    
     @Override
    public void dispose() {  
         int result=JOptionPane.showConfirmDialog(null, "Are you sure to Save and Close?");
         if(result==JOptionPane.YES_OPTION){try {
             fos.saveSystem(fos);
             } catch (IOException ex) {
                 Logger.getLogger(ManagerGUI.class.getName()).log(Level.SEVERE, null, ex);
             }
         super.dispose();}
         else if(result==JOptionPane.NO_OPTION){super.dispose();}
         
         else{}
         //To change body of generated methods, choose Tools | Templates.
    }                        

    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PaymentGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PaymentGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PaymentGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PaymentGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PaymentGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CouponButton;
    private javax.swing.JRadioButton DelieveryButton;
    private javax.swing.JLabel MoneyDeductedLable;
    private javax.swing.JRadioButton addCard;
    private javax.swing.JTextField addText;
    private javax.swing.JButton applyCreditButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    public javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.JButton calculateButton;
    private javax.swing.JButton confirmButton;
    private javax.swing.JTextField couponText;
    private javax.swing.JTextField creditApplyText;
    private javax.swing.JRadioButton existCard;
    private javax.swing.JComboBox existCombo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTextField newCardNoText;
    private javax.swing.JRadioButton pickUpButton;
    private javax.swing.JLabel totalLable;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    private FoodOrderingSystem fos;
    private Customer currentCust;
    private Order order;
    
}
